//
//  LDDatePickerView.m
//  zhilian
//
//  Created by 李丹 on 2019/7/23.
//  Copyright © 2019 LZT. All rights reserved.
//

#import "LDDatePickerView.h"
#import "LDBottomPopView.h"
@interface  LDDatePickerView()<PGDatePickerDelegate>
@property (nonatomic, strong)NSString *time;
@property (nonatomic, strong)NSString *currentDataString;
@property (nonatomic, strong)NSDateComponents *currentDataComponents;

@property (nonatomic, strong) LDBottomPopView *pop;
@end
@implementation LDDatePickerView

- (PGDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[PGDatePicker alloc] init];
        _datePicker.autoSelected = YES;
        _datePicker.rowHeight = (30);
        _datePicker.datePickerType = PGDatePickerTypeLine;
//        _datePicker.minimumDate = [NSDate date];
//        _datePicker.maximumDate = [NSDate date] ;
        _datePicker.datePickerMode = PGDatePickerModeDate;
        _datePicker.delegate = self;
        _datePicker.isHiddenMiddleText = YES;
        _datePicker.textFontOfOtherRow = TGAutoFont(14);
        _datePicker.textFontOfSelectedRow = TGAutoFont(14);
        //设置线条的颜色
        _datePicker.lineBackgroundColor = RGB(231,231,231);
        //设置选中行的字体颜色
        _datePicker.textColorOfSelectedRow = TitleBlackColor;
        //设置未选中行的字体颜色
        _datePicker.textColorOfOtherRow = RGBA(204,204,204,1);
     
    }
    return _datePicker;
}
- (void)setMinimumDate:(NSDate *)minimumDate {
    _minimumDate = minimumDate;
    self.datePicker.minimumDate = minimumDate;
}
- (void)setMaximumDate:(NSDate *)maximumDate {
    _maximumDate = maximumDate;
    self.datePicker.maximumDate = maximumDate;
}
- (instancetype)init {
    if (self = [super initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, RatioHeight(121) + TabBarHeight - 49)]) {
        [self addSubview:self.datePicker];
    }
    return self;
}
- (void)setDatePickerMode:(PGDatePickerMode)datePickerMode {
    _datePickerMode = datePickerMode;
    self.datePicker.datePickerMode = datePickerMode;
}
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    self.currentDataComponents = dateComponents;
    if (self.datePicker.datePickerMode == PGDatePickerModeDate) {
        self.currentDataString = [NSString stringWithFormat:@"%d-%.2d-%.2d",(int)dateComponents.year,(int)dateComponents.month,(int)dateComponents.day];
    }else if(self.datePicker.datePickerMode == PGDatePickerModeYearAndMonth) {
        self.currentDataString = [NSString stringWithFormat:@"%d-%.2d",(int)dateComponents.year,(int)dateComponents.month];
    }else if (self.datePicker.datePickerMode ==PGDatePickerModeDateHourMinute)
    {
        //只展示到分,但是需要显示到秒
        self.currentDataString = [NSString stringWithFormat:@"%d-%.2d-%.2d %.2d:%.2d:00",(int)dateComponents.year,(int)dateComponents.month,(int)dateComponents.day,(int)dateComponents.hour,(int)dateComponents.minute];
    }else if (self.datePicker.datePickerMode ==PGDatePickerModeDateHourMinuteSecond)
    {
        //只展示到分,但是需要显示到秒
        self.currentDataString = [NSString stringWithFormat:@"%d-%.2d-%.2d %.2d:%.2d:%.2d",(int)dateComponents.year,(int)dateComponents.month,(int)dateComponents.day,(int)dateComponents.hour,(int)dateComponents.minute,(int)dateComponents.second];
    }else if (self.datePicker.datePickerMode ==PGDatePickerModeMonthDayHourMinute)
    {
        self.currentDataString = [NSString stringWithFormat:@"%.2d-%.2d %.2d:%.2d",(int)dateComponents.month,(int)dateComponents.day,(int)dateComponents.hour,(int)dateComponents.minute];
        
    }else if (self.datePicker.datePickerMode ==PGDatePickerModeTime)
    {
        self.currentDataString = [NSString stringWithFormat:@" %.2d:%.2d",(int)dateComponents.hour,(int)dateComponents.minute];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat rowHeight = (30);
    self.datePicker.frame = CGRectMake(RatioWidth(30), AverageFloat(self.height, rowHeight*3), self.width - RatioWidth(30) * 2, rowHeight*3);
}
- (void)setCurrentDate:(NSDate *)currentDate {
    
    _currentDate = currentDate;
    if (_currentDate == nil) {
        return;
    }
    [self.datePicker setDate:currentDate animated:NO];
}
- (void)setMiddleTextStr:(NSString *)middleTextStr {
    _middleTextStr = middleTextStr;
//    self.datePicker.middleTextStr = middleTextStr;
    
}

- (void)show {
    HidenKeybory;
    WEAK
    [self.pop setSureblock:^{
        STRONG
        if (self.didSelectDateComponents) {
            self.didSelectDateComponents(self.currentDataComponents);
        }
        if (self.didSelectComplent) {
            self.didSelectComplent (self.currentDataString);
        }
    }];
    
    [self.pop setHideblock:^{
        STRONG
        UILabel *label = [self.pop viewWithTag:1000];
        label.hidden = YES;
    }];
    [self.pop show];
    if (!EmptyCheck(self.middleTextStr)) {
        self.pop.centerTitle = self.middleTextStr;
//        [self setupMiddleTextLabel];
    }
}

- (void)setupMiddleTextLabel {
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(RatioWidth(60),  SCREEN_HEIGHT-self.height-RatioHeight(63), SCREEN_WIDTH-RatioWidth(120), RatioHeight(48));
    label.text = self.middleTextStr;
    label.textColor = LightGrayColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = TGAutoFont(15);
    label.tag = 1000;
    [self.pop addSubview:label];
    
}

- (LDBottomPopView *)pop {
    if (!_pop) {
        _pop = [[LDBottomPopView alloc] initWithAlertView:self];
    }
    return _pop;
}
@end
